package com.sinosoft.ptas.control;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.api.domain.OrganizationEntity;
import com.sinosoft.framework.core.api.service.OrganizationService;
import com.sinosoft.framework.core.common.constants.ErrorConstants;
import com.sinosoft.framework.core.common.constants.OrganizationConstants;
import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.OrganizationData;

/**
 * 组织管理
 * @author lianWuJun
 * @time   2017年2月23日 上午10:39:29
 */
@Controller
@RequestMapping(value = "organization")
public class OrganizationController {

	@Autowired
	OrganizationService organizationService;

	/**
	 * 新建组织
	 * @param organizationData
	 * @return 组织传输类
	 */
	@RequestMapping(value = "/createOrganization.service", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData createOrganization(OrganizationData organizationData) {
		try {
			organizationData.setId(UUID.randomUUID().toString());
			organizationService.createOrganization(organizationData.toEntity());
			return new ResponseData(true, organizationData);
		} catch (BusinessException e) {
			return new ResponseData(false, e.getErrorCode(), e.getMessage(), organizationData);
		}
	}

	/**
	 * 获取所有组织参数
	 * @return 组织传输类集合
	 */
	@RequestMapping(value = "/retrieveOrganizations.service", method = RequestMethod.GET)
	@ResponseBody
	public ResponseData retrieveOrganizations() {
		try {
			List<OrganizationEntity> organizations = organizationService.retrieveOrganizations(null, null);
			List<OrganizationData> organizationDatas = getOrganizationDatas(organizations);
			ResponseData data = new ResponseData(true, organizationDatas);
			return data;
		} catch (BusinessException e) {
			return new ResponseData(false, e.getErrorCode(), e.getMessage(), null);
		}
	}

	/**
	 * 根据标识获取司局
	 * 0代表司局   1代表其他司
	 * @param identify
	 * @return 司局集合
	 */
	@RequestMapping(value = "/retrieveOrganizationsByIdentify.service", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveOrganizationsByIdentify(String identify) {
		try {
			List<OrganizationEntity> organizations = organizationService.retrieveOrganizationsByIdentify(identify);
			List<OrganizationData> organizationDatas = getOrganizationDatas(organizations);
			ResponseData data = new ResponseData(true, organizationDatas);
			return data;
		} catch (BusinessException e) {
			return new ResponseData(false, e.getErrorCode(), e.getMessage(), null);
		}
	}
	
	/**
	 * 查询组织参数根据组织编号
	 * @param id
	 * @return 组织传输类信息
	 */
	@RequestMapping(value = "/retrieveOrganizationById.service", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveOrganizationById(String id) {
		try {
			OrganizationEntity organizationEntity = new OrganizationEntity();
			organizationEntity = organizationService.retrieveOrganizationById(id);
			ResponseData data = new ResponseData(true, getOrganizationData(organizationEntity));
			return data;
		} catch (BusinessException e) {
			return new ResponseData(false, e.getErrorCode(), e.getMessage(), null);
		}
	}

	/**
	 * 根据组织编号删除组织
	 * @param id
	 */
	@RequestMapping(value = "/deleteOrganization.service", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteOrganization(String id) {
		try {
			organizationService.deleteOrganizationById(id);
			return new ResponseData(true, OrganizationConstants.DELETE_ORGANIZATION_SUCCESS);
		} catch (BusinessException e) {
			return new ResponseData(false, e.getErrorCode(), e.getMessage(), null);
		}
	}

	/**
	 * 根据id修改组织信息
	 * @param OrganizationData  组织信息
	 * @return 组织传输类
     */
	@RequestMapping(value = "/updateOrganization.service", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData updateOrganization(OrganizationData organizationData) {
		try {
			// 缺少更新者和更新的时间 如果加上了请删除此句话
			organizationService.updateOrganization(organizationData.toEntity());
			return new ResponseData(true, OrganizationConstants.UPDATE_ORGANIZATION_SUCCESS);
		} catch (BusinessException e) {
			return new ResponseData(false, e.getErrorCode(), e.getMessage(), null);
		}
	}

	/**
	 * 组织信息验证 是否重复 返回true 是，重复 返回false 不是，不重复
	 * @param organizationData  组织信息
	 * @return http请求回执数据
	 */
	@RequestMapping(value = "/validateOrganizationInfo.service", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData validateOrganizationInfo(OrganizationData organizationData) {
		try {
			organizationService.validateOrganizationInfo(organizationData.toEntity());
			return new ResponseData(false, ErrorConstants.SUCCESS_CODE, ErrorConstants.SUCCESS_MESSAGE, null);
		} catch (BusinessException e) {
			return new ResponseData(true, e.getErrorCode(), e.getMessage(), null);
		}
	}

	/**
	 * 
	 * 实体类集合转化传输类集合
	 * @param entitys
	 * @return
	 */
	private List<OrganizationData> getOrganizationDatas(List<OrganizationEntity> entitys) {
		int size = entitys.size();
		List<OrganizationData> OrganizationDatas = new ArrayList<OrganizationData>();
		for (int i = 0; i < size; i++) {
			OrganizationData data = new OrganizationData();
			data.toOrganizationData(entitys.get(i));
			OrganizationDatas.add(data);
		}
		return OrganizationDatas;
	}

	/**
	 * 实体类转化传输类
	 * @param entity
	 * @return data
	 */
	private OrganizationData getOrganizationData(OrganizationEntity entity) {
		OrganizationData data = new OrganizationData();
		data.toOrganizationData(entity);
		return data;
	}
}
